const { con, Result } = require('./common');
const { Getusersig } = require('./tim_mod.js');
let get_C = async (business_id) => {
	let res = new Result();
	let sql = `select * from commodity where business_id = ${business_id}`;
	let [rows] = await con.execute(sql);
	res.data = rows;
	return res;
}
let get_A = async () => {
	let res = new Result();
	let sql = `select * from commodity`;
	let [rows] = await con.execute(sql);
	res.data = rows;
	return res;
}
let Look = async () => {
	let res = new Result();
	let sql = `select * from business`;
	let [rows] = await con.execute(sql);
	res.data = rows;
	return res;
}
let Business_register = async (business_phone, business_password, password_confim) => {
	let res = new Result();
	let sql = `select * from user where user_phone = ${business_phone}`;
	let sql1 = `select * from business where business_phone = ${business_phone}`;
	let date = new Date();
	let time = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
	let business_sig = Getusersig(business_phone);
	let [rows] = await con.execute(sql);
	let [rows1] = await con.execute(sql1);
	if (rows.length == 0 && rows1.length == 0) {
		if (business_password == password_confim) {
			let sql2 = `select * from business`;
			let [rows2] = await con.execute(sql2);
			if (rows2.length == 0) {
				let sql3 = `insert business values(1,1,'a.png',${business_phone},'xxx','${business_password}','xxx',6,'xx:xx-xx:xx',NULL,'xxx','xxx','你猜我在哪','${time}',${business_sig},false)`;
				let [rows3] = await con.execute(sql3);
			} else {
				let sql3 = `insert business values(${rows2[rows2.length - 1].business_id + 1},${rows2[rows2.length - 1].business_id + 1},'a.png',${business_phone},'xxx','${business_password}','xxx',6,'xx:xx-xx:xx',NULL,'xxx','xxx','你猜我在哪','${time}','${business_sig}',false)`;
				let [rows3] = await con.execute(sql3);
			}
		} else {
			res.code = 40003;
			res.msg = '两此输入密码不一致';
		}
	} else {
		res.code = 40004;
		res.msg = '该手机号已被注册';
	}
	return res;
}
let Login = async (business_phone, business_password) => {
	let res = new Result();
	let sql = `select * from business where business_phone = ${business_phone}`;
	let [rows] = await con.execute(sql);
	if (rows.length == 1) {
		if (rows[0].business_password != business_password) {
			res.code = 40002;
			res.msg = '密码错误';
		} else {
			let sql1 = `select * from business where business_phone = ${business_phone}`;
			let [rows1] = await con.execute(sql1);
			res.data = rows1;
		}
	} else {
		res.code = 40001;
		res.msg = '电话号码未注册';
	}
	return res
}
let Get = async () => {
	let res = new Result();
	let sql = `select * from business`;
	let [rows] = await con.execute(sql);
	res.data = rows;
	return res;
}
let Modify = async (business_id, business_name, filename, filename1, business_phone, business_email, business_password, password_confim, business_header, county_id, business_time, business_describe, password,business_address) => {
	let res = new Result();
	if (business_name) {
		let sql = `update business set business_name = '${business_name}' where business_id = ${business_id}`;
		let [rows] = await con.execute(sql);
	}
	if(business_address){
		let sql = `update business set business_address = '${business_address}' where business_id = ${business_id}`;
		let [rows] = await con.execute(sql);
	}
	if (filename) {
		let sql = `update business set business_pic = '${filename}' where business_id =${business_id}`;
		let [rows] = await con.execute(sql);
	}
	if (business_header) {
		let sql = `update business set business_header = '${business_header}' where business_id = ${business_id}`;
		let [rows] = await con.execute(sql);
	}
	if (county_id) {
		let sql = `update business set county_id = ${county_id} where business_id = ${business_id}`;
		let [rows] = await con.execute(sql);
	}
	if (business_time) {
		let sql = `update business set business_time = '${business_time}' where business_id = ${business_id}`;
		let [rows] = await con.execute(sql);
	}
	if (business_describe) {
		let sql = `update business set business_describe = '${business_describe}' where business_id = ${business_id}`;
		let [rows] = await con.execute(sql);
	}
	if (business_phone) {
		let sql = `select * from user where user_phone = ${business_phone}`;
		let [rows] = await con.execute(sql);
		let sql1 = `select * from business where business_phone = ${business_phone}`;
		let [rows1] = await con.execute(sql1);
		if (rows.length == 0 && rows1.length == 0) {
			let sql2 = `select * from business where business_id = ${business_id}`;
			let [rows2] = await con.execute(sql2);
			if (rows2[0].business_password == password) {
				let sql3 = `update business set business_phone = ${business_phone} where business_id = ${business_id}`;
				let [rows3] = await con.execute(sql3);
			} else {
				res.code = 40002;
				res.msg = ' 密码错误';
			}
		} else {
			res.code = 40004;
			res.msg = '改电话号码已被注册';
		}
	}
	if (business_password) {
		let sql = `select * from business where business_id = ${business_id}`;
		let [rows] = await con.execute(sql);
		if (rows[0].business_password == password) {
			if (business_password == password_confim) {
				let sql = `update business set business_password = '${business_password}' where business_id = ${business_id}`;
				let [rows] = await con.execute(sql);
			} else {
				res.code = 40003;
				res.msg = '两次输入的密码不一致';
			}
		} else {
			res.code = 40002;
			res.msg = '密码错误';
		}
	}
	if (business_email) {
		let sql = `select * from user where user_email = '${business_email}'`;
		console.log(sql)
		let [rows] = await con.execute(sql);
		let sql1 = `select * from business where business_email = '${business_email}'`;
		let [rows1] = await con.execute(sql1);
		if (rows.length == 0 && rows1.length == 0) {
			let sql2 = `select * from business where business_id = ${business_id}`;
			let [rows2] = await con.execute(sql2);
			if (rows2[0].business_password == password) {
				let sql3 = `update business set business_email = '${business_email}' where business_id = ${business_id}`;
				let [rows3] = await con.execute(sql3);
			} else {
				res.code = 40002;
				res.msg = ' 密码错误';
			}
		} else {
			res.code = 40004;
			res.msg = '改电话号码已被注册';
		}
	}
	if (filename1) {
		console.log(filename1)
		let sql2 = `select * from business where business_id = ${business_id}`;
		let [rows2] = await con.execute(sql2);
		if (rows2[0].business_password == password) {
			let sql4 = `update business set	business_qrcode = '${filename1}' where business_id = ${business_id}`;
			let [rows4] = await con.execute(sql4);
		} else {
			res.code = 40002;
			res.msg = ' 密码错误';
		}
	}
	let sql10 = `select * from business where business_id = ${business_id}`;
	let [rows10] = await con.execute(sql10);
	res.data = rows10;
	return res;
}
let Business_Delete = async (business_id, admin_id, business_password, admin_password) => {
	let res = new Result();
	let rows5 = [];
	let rows6 = [];
	if (business_id) {
		let sql5 = `select * from business where business_id = ${business_id} and business_password = '${business_password}'`;
		[rows5] = await con.execute(sql5);
	}
	if (admin_id) {
		let sql6 = `select * from admin where admin_id = ${admin_id} and admin_password = '${admin_password}'`;
		[rows6] = await con.execute(sql6);
	}
	if (rows5.length == 1 || rows6.length == 1) {
		let sql = `select * from commodity where business_id = ${business_id}`;
		let [rows] = await con.execute(sql);
		rows.map(async (item) => {
			let sql = `delete from comment where commodityco_id = ${item.id}`;
			let [rows] = await con.exexute(sql);
			let sql1 = `delete from collection where commodity_id = ${item.id}`;
			let [rows1] = await con.execute(sql1);
			let sql2 = `delete from appointment where commoditya_id = ${item_id}`;
			let [rows2] = await con.execute(sql2);
		})
		let sql1 = `delete from commodity_temp where business_id = ${business_id}`;
		let [rows1] = await con.execute(sql1);
		let sql2 = `delete from commodity where business_id = ${business_id}`;
		let [rows2] = await con.execute(sql2);
		let sql4 = `delete from image where businessi_id = ${business_id}`;
		let [rows4] = await con.execute(sql4);
		let sql3 = `delete from business where business_id = ${business_id}`;
		let [rows3] = await con.execute(sql3);
	} else {
		res.code = 40002;
		res.msg = '密码错误';
	}
	return res;
}

module.exports = { Business_register, Login, Get, Modify, Business_Delete, get_C, get_A, Look }
